﻿using Admin.ApplicationLib.Entities;
using Admin.CommonLib.Models.Role;
using Ardalis.Specification;

namespace Admin.ApplicationLib.Specifications
{
    public sealed class RoleSpecification : Specification<RoleEntity>
    {
        public RoleSpecification(RoleSearchRequestDto model, bool isInAdmin)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            if (!isInAdmin)
            {
                Query.Where(i => !i.Code.Equals("admin"));
            }

            if (!string.IsNullOrEmpty(model.RoleName))
            {
                Query.Where(i => i.Name.Contains(model.RoleName));
            }

            if (model.Page > 0)
            {
                Query.Skip((model.Page - 1) * model.Limit);
            }

            if (model.Limit > 0)
            {
                Query.Take(model.Limit);
            }

            Query.OrderByDescending(i => i.CreatedOn);
        }
    }
}